package com.example.liming39.myapplication.feature;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.android.volley.toolbox.StringRequest;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * @author liming356
 * @since 2018/10/18/018
 */
public class HttpJson {

    JSONObject postJson = null, getJson = null;

    public void setPostJson(JSONObject postJson) {
        this.postJson = postJson;
    }

    public JSONObject getPostJson() {
        return postJson;
    }

    public JSONObject getGetJson() {
        return getJson;
    }

    public void setGetJson(JSONObject getJson) {
        this.getJson = getJson;
    }

    public void HttpJsonPost(final JSONObject jsonObject, String url) {
        JsonRequest<JSONObject> jsonRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        postJson = response;
                        System.out.println("HttpJsonPost请求信息" + response.toString());
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
//                logger.error(error.getStackTrace().toString() + jsonObject.toString());
//                Log.e("HttpJsonPost请求", error.getMessage() + jsonObject.toString(), error);
            }
        });
        MyApp.requestQueue.add(jsonRequest);
        MyApp.requestQueue.start();
    }

    public void HttpJsonGet(String url) {

//        Log.d("JsonGet请求：", "调用了");
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
//                Log.d("JsonGet请求信息：", response.toString());
                getJson = response;
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
//                Log.e("JsonGet请求出错：", error.getMessage(), error);
//                logger.error("JsonGet请求出错：" + error.getMessage());
            }
        });
        MyApp.requestQueue.add(jsonObjectRequest);
        MyApp.requestQueue.start();
    }

    public void HttpStringGet(String url) {
//        Log.d("StringGet请求", "调用了");
        StringRequest stringRequest = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
//                        Log.d("StringGet请求信息", response.toString());
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
//                Log.e("StringGet请求出错", error.getMessage(), error);
            }
        });
        MyApp.requestQueue.add(stringRequest);
        MyApp.requestQueue.start();
    }

    public void HttpStringPost(String url) {
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
//                Log.d("PostString", response.toString());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
//                Log.d("PostString", error.toString().trim());
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                map.put("name", "朝阳");
                map.put("keyword", "我的天呐");
                //请求消息体
                return map;
            }
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                map.put("city", "%E4%B8%B4%E6%B2%82");
                return map;
            }

        };
        MyApp.requestQueue.add(stringRequest);
        MyApp.requestQueue.start();
    }

}
